#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>  
#include <string>  
using namespace std;

struct Student {
    string name;
    int age;
    string id;
};

// Function to add student  
void addStudent(Student students[], int& length, const string& name, int age, const string& id) {
    if (length >= 100) {
        cout << "Database full, cannot add more students." << endl;
    }
    else {
        students[length].name = name;
        students[length].age = age;
        students[length].id = id;
        length++;
        cout << "Student added successfully!" << endl;
    }
}

// Function to display students  
void displayStudents(const Student students[], int length) {
    cout << "Current list of students:" << endl;
    for (int i = 0; i < length; i++) {
        cout << "Name: " << students[i].name << ", Age: " << students[i].age << ", ID: " << students[i].id << endl;
    }
}

// Function to delete student  
void deleteStudent(Student students[], int& length, const string& id) {
    int position = -1;
    for (int i = 0; i < length; i++) {
        if (students[i].id == id) {
            position = i;
            break;
        }
    }
    if (position == -1) {
        cout << "Student not found!" << endl;
    }
    else {
        for (int i = position; i < length - 1; i++) {
            students[i] = students[i + 1];
        }
        length--;
        cout << "Student deleted successfully!" << endl;
    }
}

// Function to update student information  
void updateStudent(Student students[], int length, const string& id, const string& newName, int newAge) {
    bool found = false;
    for (int i = 0; i < length; i++) {
        if (students[i].id == id) {
            students[i].name = newName;
            students[i].age = newAge;
            cout << "Student updated successfully!" << endl;
            found = true;
            break;
        }
    }
    if (!found) {
        cout << "Student not found!" << endl;
    }
}

int main() {
    Student students[100];
    int length = 0;
    int choice;
    string name, id, newName;
    int age, newAge;

    while (true) {
        cout << "1. Add Student" << endl;
        cout << "2. Display Students" << endl;
        cout << "3. Delete Student" << endl;
        cout << "4. Update Student" << endl;
        cout << "5. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
        case 1:
            cout << "Enter name: ";
            cin >> name;
            cout << "Enter age: ";
            cin >> age;
            cout << "Enter ID: ";
            cin >> id;
            addStudent(students, length, name, age, id);
            break;
        case 2:
            displayStudents(students, length);
            break;
        case 3:
            cout << "Enter ID of the student to delete: ";
            cin >> id;
            deleteStudent(students, length, id);
            break;
        case 4:
            cout << "Enter ID of the student to update: ";
            cin >> id;
            cout << "Enter new name: ";
            cin >> newName;
            cout << "Enter new age: ";
            cin >> newAge;
            updateStudent(students, length, id, newName, newAge);
            break;
        case 5:
            cout << "Exiting program." << endl;
            return 0;
        default:
            cout << "Invalid choice, please try again." << endl;
        }
    }

    return 0;
}